<?php
namespace app\api\controller;

use app\BaseController;
use app\console\model\Config;
use app\model\DeviceSigning as SelfModel;

use app\model\LeasingCompany;
use app\model\Project;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\Cookie;
use think\Response;
use think\Request;
class DeviceSigning extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\DeviceSigning";
        $this->list_rows = 10;
        $this->index = ['id','title','id_no','name_no','project_title','price','balance','start_time','end_time','delay_time','create_time'];
        $this->all = ['id','device_id','device_name','before','after','start_time','end_time','create_time'];
        $this->my = ['id','device_id','device_name','before','after','start_time','end_time','create_time'];
        $this->view = ['id','device_id','device_name','before','after','start_time','end_time','create_time'];
        $this->msg = '签约记录';
        
        
    }

    public function index(Request $request)
    {
        $where = [];
        if(!empty(input('start_time')) && !empty($request->end_time)){
            $where[] = ['create_time','between',[input('start_time'),$request->end_time]];
        }


        $data = $this->model::withSearch(array_keys($request->data), $request->data)
            ->field($this->index)
//            ->where($where)
            ->order(['id' => 'desc'])
            ->paginate($this->list_rows)->each(function ($item,$key){
//                $item['price'] = price(Config::where('varname','year_free')->value('varvalue'));
            })->hidden(['before']);
       // echo $this->model::getlastsql();
        return ['code' => 200, 'msg' =>  '平台年费报表', 'data' => $data];
    }

    public function export(Request $request)
    {

        $list =  $data = $this->model::withSearch(array_keys($request->data), $request->data)
            ->field($this->index)
//                ->withoutField($this->without_field)
//                ->where($this->status)
            ->where($this->where)
            ->field(['id','project_title','title','id_no','name_no','price','create_time'])
//            ->order($this->order)
            ->select();

        $spreadsheet = new Spreadsheet();
//        $spreadsheet->setActiveSheetIndex(0);
        $sheet = $spreadsheet->getActiveSheet()->setTitle('年费报表');
        $title_arg = ['ID','序号','铭牌号','设备号','项目名称','扣款金额','余额','年费开始时间','年费结束时间','年费延迟时间','扣款日期'];
        for ($i=1;$i<=count($title_arg);$i++){
            $sheet->setCellValueByColumnAndRow($i,1,  $title_arg[$i-1]);
        }

        foreach ($list as $k => $v) {
            $j = $k + 2;
            $sheet->setCellValueByColumnAndRow(1, $j, $v['id']);
            $sheet->setCellValueByColumnAndRow(2, $j, $v['id_no']);
            $sheet->setCellValueByColumnAndRow(3, $j, $v['name_no']);
            $sheet->setCellValueByColumnAndRow(4, $j, $v['title']);
            $sheet->setCellValueByColumnAndRow(5, $j, $v['project_title']);
            $sheet->setCellValueByColumnAndRow(6, $j, $v['price']);
            $sheet->setCellValueByColumnAndRow(7, $j, $v['balance']);
            $sheet->setCellValueByColumnAndRow(8, $j, $v['start_time']);
            $sheet->setCellValueByColumnAndRow(9, $j, $v['end_time']);
            $sheet->setCellValueByColumnAndRow(10, $j, $v['delay_time']);
            $sheet->setCellValueByColumnAndRow(11, $j, $v['create_time']);

        }

        $name = '年费报表:';
        $company_name = LeasingCompany::where('id',$request->company_id)->value('title');
        $project_name = Project::where('id',$request->project_id)->value('title');
        if(!empty($company_name)){
            $name .=$company_name;
        }
        if(!empty($project_name)){
            $name .= '-'.$project_name;
        }
        if(!empty($request->start_time)){
            $name .= '-'.$request->start_time;
        }
        if(!empty($request->end_time)){
            $name .= '-'.$request->end_time;
        }
        $writer = new Xlsx($spreadsheet);
//        $name=rand(10,99);
        $writer->save('./excel/'.$name.'.xlsx');
        return ['code'=>200,'msg'=>'平台年费报表导出','url'=> $request->domain().'/excel/'.$name.'.xlsx'];
    }






}
